package com.project.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.project.common.Constants;
import com.project.entity.sys.User;
import com.project.service.sys.UserService;
import com.project.util.SpringContextUtil;

/**
 * springMVC 访问拦截
 * @author 
 */
public class CommonInterceptor extends  HandlerInterceptorAdapter {

	 @Override
	 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
		 /*url判断*/
		 String menuUrlSign = request.getParameter("menuUrlSign");
		 if("1".equals(menuUrlSign)){
			 request.getSession().setAttribute("menuUrl", request.getRequestURI());
			 request.setAttribute("menuUrl", request.getRequestURI());
		 }else{
			 request.setAttribute("menuUrl", request.getSession().getAttribute("menuUrl"));
		 }
		 
		 /*当使用中的用户被禁用则立马退出*/
		 User user = (User) request.getSession().getAttribute("currentUser");
		 
		 if(user !=null){
			 UserService userService = (UserService) SpringContextUtil.getBean("userService");
			 User currentUser = userService.getById(user.getId());
			 
			 if(Constants.STATUS_CLOSE.equals(currentUser.getStatus())){//如果被禁用则登出
				 SecurityUtils.getSubject().logout();
				 
				 String root = request.getContextPath();    
				 response.sendRedirect(root+"/login/init");
				 return false;
			 }
		 }
		 
		 return true;
	 }

}
